<?php
namespace app\api\controller;
/**
 * 邮箱-API接口
 * User: huangzhen
 * Date: 2021/01/14
 */

use think\facade\Request;
use think\facade\Db;
use PHPMailer\PHPMailer\PHPMailer;


class DecEmailSend
{
    /**
     * 构造方法
     * @param Request $request Request对象
     */
    public function __construct(Request $request)
    {
    }

    public static function sendEmail($data)
    {
        return self::sends($data);
    }

    public static function sends($data)
    {
        $user_id    = $data['user_id'];
        $email      = $data['email'];
        $desc       = $data['desc'];
        $card_num   = $data['card_num'];
        $amount         = $data['amount'];
        $amount_rate    = $data['amount_rate'];
        $dec_rate = Db::name('user')->where(['id'=>$user_id])->value('dec_rate');
        $dec_rate_b = $dec_rate*100 . " %";
        $real_amount = getSprintf($amount_rate + round($amount * $dec_rate , 2));
        
        $desc      .= "<br/><br/>为了防止拒付，请广大用户保证卡片余额充足。对于不想继续的订阅，及时取消。常见软件取消订阅的方法如下：";
        $desc      .= "<br/>https://www.vpsdawanjia.com/howtocancelyourplan";
        $desc      .= "<br/>如果没有取消订阅，并且卡片余额不足，余额不足就会导致产生拒付记录.";
        $desc      .= "<br/>帐号余额和卡片余额是独立的，请充值到卡上以后再消费.";
        $desc      .= "<br/>对于拒付，平台的处理办法如下：";
        $desc      .= "<br/>如果出现通过我们虚拟卡在Facebook平台的交易被拒付的情况，我们将收取<span style=\"font-size: 21px;color: red;font-weight: bold\">0.3美元/笔+ " . $dec_rate_b ." *拒付金额</span>的费用；在其他非Facebook平台发生的被拒付交易，我们将收取<span style=\"font-size: 21px;color: red;font-weight: bold\">0.2美元/笔+ " . $dec_rate_b ." *拒付金额</span>的费用。<span style=\"font-size: 21px;color: red;font-weight: bold\">本单拒付的手续费为：" . $amount_rate . " + " . $dec_rate_b ." * " . $amount . " = " . $real_amount . " </span>。交易手续费将从您的账号余额扣除，您可以在“财务明细”菜单查看相关的扣费记录。这部分收费不是我们主动提出，而是因为交易所涉及的银行和网络将这部分拒付和零金额交易手续费转嫁给我们，我们也不得不进行必要的成本分摊。";
        $desc      .= "<br/>请广大用户重视拒付交易记录，如不同意本处理办法，请勿使用本平台提供的服务.";
        $desc      .= "<br/>请勿直接回复本邮件，客服邮箱：support@cardecho.com.";

        $request = "email：{$email} ";
        $Subject = $data['title'];
        $body    = $desc;

        $mail = new PHPMailer();
        $mail->isSMTP(); // 启用SMTP
        $mail->Host     ="smtp.gmail.com"; //smtp服务器的名称（这里以QQ邮箱为例）
        $mail->SMTPSecure = "ssl"; //目前规定必须使用ssl，非ssl的协议已经不支持了
        $mail->Port     = 465; //端口号
        //$mail->SMTPDebug = 2; //用于debug PHPMailer信息
        $mail->SMTPAuth = true; //启用smtp认证
        $mail->Username = "verify@cardecho.com"; //你的邮箱名
        $mail->Password = 'cecceytiohekfxba' ; //邮箱授权码，注意是授权码，不是登录密码
        $mail->From     = "verify@cardecho.com"; //发件人地址（也就是你的邮箱地址）
        $mail->FromName = "CardEcho"; //发件人姓名
        $mail->AddAddress($email); //收件人地址
        $mail->WordWrap = 50; //设置每行字符长度
        $mail->IsHTML(true); // 是否HTML格式邮件
        $mail->CharSet  ="utf-8"; //设置邮件编码
        $mail->Subject  = $Subject;  //添加该邮件的主题
        $mail->Body     = $body;
        $result = $mail->Send();
        $blog   = true;
        $log_code   = 200;
        $response = 'ok';
        if(!$result){
            $log_code   = 500;
            $blog   = false;
            $response = $mail->ErrorInfo;
        }
        Db::name('api_email')->save([
            'user_id' => $user_id,
            'ip'      => ip(),
            'header'  => $Subject,
            'card_num'=> $card_num,
            'request' => $request,
            'code'    => $log_code,
            'response'=> $response,
            'desc'    => $desc,
            'ctime'   => getUtcTime()
        ]);
        return $blog;
    }

    public static function sendGridDecEmail($data)
    {
        $user_id    = $data['user_id'];
        $email      = $data['email'];
        $desc       = $data['desc'];
        $save_desc  = $data['desc'];
        $title      = $data['title'] ?? 'text';
        $card_num   = $data['card_num'];
        $amount         = $data['amount'];
        $amount_rate    = $data['amount_rate'];
        $dec_rate = Db::name('user')->where(['id'=>$user_id])->value('dec_rate');
        $dec_rate_b = $dec_rate*100 . " %";
        $real_amount = getSprintf($amount_rate + round($amount * $dec_rate , 2));

        $desc      .= "<br/><br/>为了防止拒付，请广大用户保证卡片余额充足。对于不想继续的订阅，及时取消。常见软件取消订阅的方法如下：";
        $desc      .= "<br/>https://www.vpsdawanjia.com/howtocancelyourplan";
        $desc      .= "<br/>如果没有取消订阅，并且卡片余额不足，余额不足就会导致产生拒付记录.";
        $desc      .= "<br/>帐号余额和卡片余额是独立的，请充值到卡上以后再消费.";
        $desc      .= "<br/>对于拒付，平台的处理办法如下：";
        $desc      .= "<br/>如果出现通过我们虚拟卡在Facebook平台的交易被拒付的情况，我们将收取<span style=\"font-size: 21px;color: red;font-weight: bold\">0.3美元/笔+ " . $dec_rate_b ." *拒付金额</span>的费用；在其他非Facebook平台发生的被拒付交易，我们将收取<span style=\"font-size: 21px;color: red;font-weight: bold\">0.2美元/笔+ " . $dec_rate_b ." *拒付金额</span>的费用。<span style=\"font-size: 21px;color: red;font-weight: bold\">本单拒付的手续费为：" . $amount_rate . " + " . $dec_rate_b ." * " . $amount . " = " . $real_amount . " </span>。交易手续费将从您的账号余额扣除，您可以在“财务明细”菜单查看相关的扣费记录。这部分收费不是我们主动提出，而是因为交易所涉及的银行和网络将这部分拒付和零金额交易手续费转嫁给我们，我们也不得不进行必要的成本分摊。";
        $desc      .= "<br/>请广大用户重视拒付交易记录，如不同意本处理办法，请勿使用本平台提供的服务.";
        $desc      .= "<br/>请勿直接回复本邮件，客服邮箱：support@vvacard.com.";

        $apiKey     = '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';
        $sg         = new SendGridService($apiKey);

        $resp = $sg->sendMail(
            $email,
            $title,
            $desc,
            ['email'=>'verify@cardecho.com','name'=> 'CardEcho Support']
        );
        $blog = true;
        $log_code   = $resp->statusCode();
        if(substr($resp->statusCode(),0,1) != '2'){
            $save_desc .= '__' . $resp->body();
            $blog = false;
        }
        Db::name('api_email')->save([
            'user_id' => $user_id,
            'ip'      => ip(),
            'header'  => $title,
            'card_num'=> $card_num,
            'request' => $email,
            'code'    => $log_code,
            'response'=> $blog,
            'desc'    => $save_desc,
            'ctime'   => getUtcTime()
        ]);
        return $blog;
    }
}